import {Component, EventEmitter, Input, OnInit, Output, ViewChild} from '@angular/core';
import {STColumn} from '@delon/abc';
import {SFSchema} from '@delon/form';
import {NzMessageService} from 'ng-zorro-antd';
import {SysCommandRunLogService} from '../service/sys-command-run-log.service';
import {BaseGridComponent, BaseService} from '@sb/base';
import {SysCommandRunLogSearchForm} from '../entity/sys-command-run-log-search-form';

@Component({
    selector: 'sys-command-run-log',
    templateUrl: './sys-command-run-log.component.html',
    styleUrls: ['./sys-command-run-log.component.less'],
    providers: [
        {
            provide: BaseService,
            useClass: SysCommandRunLogService
        }
    ]
})
export class SysCommandRunLogComponent implements OnInit {

    private _init = false;

    @Input()
    showBack = false;

    searchForm: SysCommandRunLogSearchForm = {};

    columns: STColumn[] = [
        {
            title: '',
            type: 'checkbox',
            index: 'id'
        }, {
            title: '名称',
            index: 'commandName',
            className: 'word-wrap',
            width: '150px'
        }, {
            index: 'createTime',
            title: '执行时间',
            className: 'text-center',
            width: '180px'
        }, {
            title: '命令',
            index: 'command',
            className: 'word-wrap',
            width: '35%'
        }, {
            title: '执行日志',
            index: 'logs',
            className: 'word-wrap',
            width: '35%',
            format: item => {
                let v = item.logs;
                if (v.length > 300) {
                    v = v.substring(0, 300);
                }
                return v;
            }
        }, {
            title: '操作区',
            index: 'operate',
            className: 'text-center',
            width: '80px'
        }
    ];

    schema: SFSchema = {
        properties: {
            createTime: {
                type: 'string',
                title: '执行时间'
            },
            commandName: {
                type: 'string',
                title: '命令名称',
                minLength: 2
            },
            command: {
                type: 'string',
                title: '命令',
                ui: {
                    widget: 'textarea',
                    autosize: {minRows: 4, maxRows: 20}
                }
            },
            logs: {
                type: 'string',
                title: '执行日志',
                ui: {
                    widget: 'textarea',
                    autosize: {minRows: 4, maxRows: 20}
                }
            },
        },
        required: ['commandName', 'command']
    };

    private _commandId;

    get commandId() {
        return this._commandId;
    }

    @Input()
    set commandId(value) {
        this._commandId = value;
        this.searchForm.commandId = value;
        this.reload();
    }

    @Output()
    backClick = new EventEmitter<boolean>();

    @ViewChild(BaseGridComponent, {static: true})
    bg: BaseGridComponent;

    constructor(
        private message: NzMessageService) {
    }

    ngOnInit(): void {
        this._init = true;
    }

    reload() {
        if (this.bg && this._init) {
            this.bg.reload();
        }
    }
}
